package myta.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;
import myta.service.Utility;

public class SiswaData extends FormDataBase{
	private HtmlInputText txtnis;
	private HtmlInputText txtnama;
	private HtmlInputText txtalamat;
	private HtmlInputText txttelepon;
	private HtmlSelectOneMenu txtjeniskelamin;
	private HtmlInputText txttmplahir;
	private HtmlInputText txttgllahir;
	private HtmlSelectOneMenu txtagama;
	private HtmlInputText txttglmasuk;
	private HtmlInputText txtasalsekolah;
	private HtmlInputText txtusername;
	private HtmlInputText txtpassword;
	private HtmlInputText txtnamaortu;
	private HtmlInputText txtalamatortu;
	private HtmlInputText txtteleponortu;
	private HtmlInputText txtpekerjaanortu;
	private HtmlInputText txtpendidikanterakhirortu;
	
	private List<SelectItem> listAgama;
	private List<SelectItem> listJenisKelamin;
	
	@Override
	public void init() throws Exception {
		formName = "siswa";
		
		txtnis = new HtmlInputText();
		txtnama = new HtmlInputText();
		txtalamat = new HtmlInputText();
		txttelepon = new HtmlInputText();
		txtjeniskelamin = new HtmlSelectOneMenu();
		txttmplahir = new HtmlInputText();
		txttgllahir = new HtmlInputText();
		txtagama = new HtmlSelectOneMenu();
		txttglmasuk = new HtmlInputText();
		txtasalsekolah = new HtmlInputText();
		txtusername = new HtmlInputText();
		txtpassword = new HtmlInputText();
		txtnamaortu = new HtmlInputText();
		txtalamatortu = new HtmlInputText();
		txtteleponortu = new HtmlInputText();
		txtpekerjaanortu = new HtmlInputText();
		txtpendidikanterakhirortu = new HtmlInputText();
		
		listAgama = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem();
		item1.setValue("Islam");
		item1.setLabel("Islam");
		SelectItem item2 = new SelectItem();
		item2.setValue("Kristen");
		item2.setLabel("Kristen");
		SelectItem item3 = new SelectItem();
		item3.setValue("Katolik");
		item3.setLabel("Katolik");
		SelectItem item4 = new SelectItem();
		item4.setValue("Budha");
		item4.setLabel("Budha");
		SelectItem item5 = new SelectItem();
		item5.setValue("Hindu");
		item5.setLabel("Hindu");
		
		listAgama.add(item1);
		listAgama.add(item2);
		listAgama.add(item3);
		listAgama.add(item4);
		listAgama.add(item5);
		
		listJenisKelamin = new ArrayList<SelectItem>();
		SelectItem iteml = new SelectItem();
		iteml.setValue("L");
		iteml.setLabel("Laki-laki");
		
		SelectItem itemp = new SelectItem();
		itemp.setValue("P");
		itemp.setLabel("Perempuan");
		
		listJenisKelamin.add(iteml);
		listJenisKelamin.add(itemp);
		
//		txtnis.setDisabled(true);
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call nis()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtnis.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("nis")){
			String nis = map.get("nis");
			
			String sql = "call tampil_siswa(?)";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, nis);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtnis.setValue(datarow.getString("nis"));
			txtnama.setValue(datarow.getString("nama"));
			txtalamat.setValue(datarow.getString("alamat"));
			txttelepon.setValue(datarow.getString("telepon"));
			txtjeniskelamin.setValue(datarow.getString("jenis_kelamin"));
			txttmplahir.setValue(datarow.getString("tmp_lahir"));
			txttgllahir.setValue(Utility.format(datarow.getDate("tgl_lahir")));
			txtagama.setValue(datarow.getString("agama"));
			txttglmasuk.setValue(Utility.format(datarow.getDate("tgl_masuk")));
			txtasalsekolah.setValue(datarow.getString("asal_sekolah"));
			txtusername.setValue(datarow.getString("username"));
			txtpassword.setValue(datarow.getString("passwd"));
			txtnamaortu.setValue(datarow.getString("nama_orangtua"));
			txtalamatortu.setValue(datarow.getString("alamat_orangtua"));
			txtteleponortu.setValue(datarow.getString("telepon_orangtua"));
			txtpekerjaanortu.setValue(datarow.getString("pekerjaan_orangtua"));
			txtpendidikanterakhirortu.setValue(datarow.getString("pendidikan_terakhir_orangtua"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "insert into siswa(nis,nama,alamat,telepon,jenis_kelamin,tmp_lahir,tgl_lahir,agama,tgl_masuk,asal_sekolah,username,passwd) values(?,?,?,?,?,?,?,?,?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnis.getValue().toString());
		command.setObject(2, txtnama.getValue().toString());
		command.setObject(3, txtalamat.getValue().toString());
		command.setObject(4, txttelepon.getValue().toString());
		command.setObject(5, txtjeniskelamin.getValue().toString());
		command.setObject(6, txttmplahir.getValue().toString());
		command.setObject(7, txttgllahir.getValue().toString());
		command.setObject(8, txtagama.getValue().toString());
		command.setObject(9, txttglmasuk.getValue().toString());
		command.setObject(10, txtasalsekolah.getValue().toString());
		command.setObject(11, txtusername.getValue().toString());
		command.setObject(12, txtpassword.getValue().toString());
		boo = command.executeUpdate();
		
		if(!boo){
			return false;
		}
		
		sql = "insert into orangtua(nis,nama,alamat,telepon,pekerjaan,pendidikan_terakhir) values(?,?,?,?,?,?);";
		command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnis.getValue().toString());
		command.setObject(2, txtnamaortu.getValue().toString());
		command.setObject(3, txtalamatortu.getValue().toString());
		command.setObject(4, txtteleponortu.getValue().toString());
		command.setObject(5, txtpekerjaanortu.getValue().toString());
		command.setObject(6, txtpendidikanterakhirortu.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "update siswa set nama=?,alamat=?,telepon=?,jenis_kelamin=?,tmp_lahir=?,tgl_lahir=?,agama=?,tgl_masuk=?,asal_sekolah=?,username=?,passwd=? where nis = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnama.getValue().toString());
		command.setObject(2, txtalamat.getValue().toString());
		command.setObject(3, txttelepon.getValue().toString());
		command.setObject(4, txtjeniskelamin.getValue().toString());
		command.setObject(5, txttmplahir.getValue().toString());
		command.setObject(6, txttgllahir.getValue().toString());
		command.setObject(7, txtagama.getValue().toString());
		command.setObject(8, txttglmasuk.getValue().toString());
		command.setObject(9, txtasalsekolah.getValue().toString());
		command.setObject(10, txtusername.getValue().toString());
		command.setObject(11, txtpassword.getValue().toString());
		command.setObject(12, txtnis.getValue().toString());
		boo = command.executeUpdate();
		
		if(!boo){
			return false;
		}
		
		sql = "update orangtua set nama=?,alamat=?,telepon=?,pekerjaan=?,pendidikan_terakhir=? where nis = ?;";
		command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnamaortu.getValue().toString());
		command.setObject(2, txtalamatortu.getValue().toString());
		command.setObject(3, txtteleponortu.getValue().toString());
		command.setObject(4, txtpekerjaanortu.getValue().toString());
		command.setObject(5, txtpendidikanterakhirortu.getValue().toString());
		command.setObject(6, txtnis.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtnis() {
		return txtnis;
	}

	public void setTxtnis(HtmlInputText txtnis) {
		this.txtnis = txtnis;
	}

	public HtmlInputText getTxtnama() {
		return txtnama;
	}

	public void setTxtnama(HtmlInputText txtnama) {
		this.txtnama = txtnama;
	}

	public HtmlInputText getTxtalamat() {
		return txtalamat;
	}

	public void setTxtalamat(HtmlInputText txtalamat) {
		this.txtalamat = txtalamat;
	}

	public HtmlInputText getTxttelepon() {
		return txttelepon;
	}

	public void setTxttelepon(HtmlInputText txttelepon) {
		this.txttelepon = txttelepon;
	}

	public HtmlSelectOneMenu getTxtjeniskelamin() {
		return txtjeniskelamin;
	}

	public void setTxtjeniskelamin(HtmlSelectOneMenu txtjeniskelamin) {
		this.txtjeniskelamin = txtjeniskelamin;
	}

	public HtmlInputText getTxttmplahir() {
		return txttmplahir;
	}

	public void setTxttmplahir(HtmlInputText txttmplahir) {
		this.txttmplahir = txttmplahir;
	}

	public HtmlInputText getTxttgllahir() {
		return txttgllahir;
	}

	public void setTxttgllahir(HtmlInputText txttgllahir) {
		this.txttgllahir = txttgllahir;
	}

	public HtmlSelectOneMenu getTxtagama() {
		return txtagama;
	}

	public void setTxtagama(HtmlSelectOneMenu txtagama) {
		this.txtagama = txtagama;
	}

	public HtmlInputText getTxttglmasuk() {
		return txttglmasuk;
	}

	public void setTxttglmasuk(HtmlInputText txttglmasuk) {
		this.txttglmasuk = txttglmasuk;
	}

	public HtmlInputText getTxtasalsekolah() {
		return txtasalsekolah;
	}

	public void setTxtasalsekolah(HtmlInputText txtasalsekolah) {
		this.txtasalsekolah = txtasalsekolah;
	}

	public HtmlInputText getTxtusername() {
		return txtusername;
	}

	public void setTxtusername(HtmlInputText txtusername) {
		this.txtusername = txtusername;
	}

	public HtmlInputText getTxtpassword() {
		return txtpassword;
	}

	public void setTxtpassword(HtmlInputText txtpassword) {
		this.txtpassword = txtpassword;
	}

	public HtmlInputText getTxtnamaortu() {
		return txtnamaortu;
	}

	public void setTxtnamaortu(HtmlInputText txtnamaortu) {
		this.txtnamaortu = txtnamaortu;
	}

	public HtmlInputText getTxtalamatortu() {
		return txtalamatortu;
	}

	public void setTxtalamatortu(HtmlInputText txtalamatortu) {
		this.txtalamatortu = txtalamatortu;
	}

	public HtmlInputText getTxtteleponortu() {
		return txtteleponortu;
	}

	public void setTxtteleponortu(HtmlInputText txtteleponortu) {
		this.txtteleponortu = txtteleponortu;
	}

	public HtmlInputText getTxtpekerjaanortu() {
		return txtpekerjaanortu;
	}

	public void setTxtpekerjaanortu(HtmlInputText txtpekerjaanortu) {
		this.txtpekerjaanortu = txtpekerjaanortu;
	}

	public HtmlInputText getTxtpendidikanterakhirortu() {
		return txtpendidikanterakhirortu;
	}

	public void setTxtpendidikanterakhirortu(HtmlInputText txtpendidikanterakhirortu) {
		this.txtpendidikanterakhirortu = txtpendidikanterakhirortu;
	}

	public List<SelectItem> getListAgama() {
		return listAgama;
	}

	public void setListAgama(List<SelectItem> listAgama) {
		this.listAgama = listAgama;
	}

	public List<SelectItem> getListJenisKelamin() {
		return listJenisKelamin;
	}

	public void setListJenisKelamin(List<SelectItem> listJenisKelamin) {
		this.listJenisKelamin = listJenisKelamin;
	}

}
